<template>
	<div class="sys-notice-container">
		<el-dialog v-model="state.isShowDialog" draggable :close-on-click-modal="false" width="900px">
			<template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit /> </el-icon>
					<span> 新增项目 </span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="项目名称" prop="name" :rules="[{ required: true, message: '项目名称不为空', trigger: 'blur' }]">
							<el-input v-model="state.ruleForm.name" placeholder="输入项目名称" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="客户" prop="customer">
                            <el-input v-model="state.ruleForm.customer" placeholder="填写客户" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="所属部门" prop="department">
                            <el-select v-model="state.ruleForm.department"  placeholder="选择部门" style="width: 100%">
								<el-option v-for="item in departments"  :key="item.id" :label="item.name" :value="item.id"> </el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="项目开始时间">
							<el-date-picker v-model="state.ruleForm.startTime" type="datetime" placeholder="起始时间" style="width: 100%" format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss" />
						</el-form-item>
					</el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="项目结束时间">
							<el-date-picker v-model="state.ruleForm.endTime" type="datetime" placeholder="结束时间" style="width: 100%" format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss" />
						</el-form-item>
					</el-col>
					<el-col :xs="24"  :sm="24" :md="24" :lg="24" :xl="24" class="mb20" v-for="(item, index)  in state.ruleForm.projectMember">
						<el-form-item :label="item.name">
							<el-select :disabled="!state.ruleForm.department ||state.ruleForm.department.length==0" v-model="item.userid" multiple filterable remote reserve-keyword
								:remote-method="remoteMethod" placeholder="选择员工" style="width: 100%">
								<el-option-group v-for="group in orgsworker" :key="group.label" :label="group.label">
									<el-option v-for="item in group.options" :key="item.value" :label="item.label"
										:value="item.value" />
								</el-option-group>
								<!-- <el-option v-for="elem in departments" :key="elem.id" :label="elem.name" :value="elem.id">
								</el-option> -->
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">取 消</el-button>
					<el-button type="primary" @click="submit">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup name="bindWorker">
import { reactive, ref } from 'vue';
import {addOrUpdateProject,GetProjectDetail,GetProjectDepartment} from '/@/api/business/project'
import { getAPI } from '/@/utils/axios-utils';
import { SysOrgApi, SysDictDataApi } from '/@/api-services/api';
import { GetOrgDepartment, GetProjectDepartment as GetProjectWorkDepartment } from '/@/api/business/ProjectWorker'

const departments = ref()
const members  = ref()
const orgsworker = ref()

const props = defineProps({
});
const emits = defineEmits(['handleQuery']);
const ruleFormRef = ref();
const state = reactive({
	isShowDialog: false,
	ruleForm: {} as Object,
});
// 打开弹窗
const openDialog =async (row: any) => {
	state.ruleForm = Object.assign({});
	await GetOrgDep();
	await GetProjectDep().then(()=>{
		state.isShowDialog = true;
	});
	// ruleFormRef.value?.resetFields();
};

const GetOrgDep = async()=>{
	const res = await GetOrgDepartment(); 
    departments.value =  res.data.result??[];
}
const GetProjectDep = async()=>{
	const res  = await GetProjectDepartment();
	state.ruleForm.projectMember = res.data.result??[];
}

const GetDetail = async(id:any)=>{
	const detail = await GetProjectDetail({id});
	state.ruleForm  =  detail.data.result;
	console.log(detail);
}



// 关闭弹窗
const closeDialog = () => {
	emits('handleQuery');
	state.isShowDialog = false;
};

// 取消
const cancel = () => {
	state.isShowDialog = false;
};

const remoteMethod = async (query: string) => {
	let param = {
		name: query,
		companys: state.ruleForm.department
	}
	const orgs = await GetProjectWorkDepartment(param);
	orgsworker.value = orgs.data.result ?? [];	
	console.log(orgs);
	// if (query) {
	// 	setTimeout(() => {
	// 		loading.value = false
	// 		options.value = list.value.filter((item) => {
	// 			return item.label.toLowerCase().includes(query.toLowerCase())
	// 		})
	// 	}, 200)
	// } else {
	// 	options.value = []
	// }
}
// 提交
const submit = () => {
	ruleFormRef.value.validate(async (valid: boolean) => {
		if (!valid) return;
        await addOrUpdateProject(state.ruleForm)
		// if (state.ruleForm.id != undefined && state.ruleForm.id > 0) {
		// 	await getAPI(SysNoticeApi).apiSysNoticeUpdatePost(state.ruleForm);
		// } else {
		// 	await getAPI(SysNoticeApi).apiSysNoticeAddPost(state.ruleForm);
		// }
		closeDialog();
	});
};

// 导出对象
defineExpose({ openDialog });
</script>
